<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 商品分类模型
 */
class Category extends ModelBase
{
    // 表名

    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    
    // 时间字段
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 字段类型转换
    protected $type = [
        'id' => 'integer',
        'parent_id' => 'integer',
        'level' => 'integer',
        'sort' => 'integer',
        'status' => 'integer',
        'create_time' => 'integer',
        'update_time' => 'integer',
    ];
    
    /**
     * 获取创建时间格式化
     */
    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
    
    /**
     * 获取更新时间格式化
     */
    public function getUpdateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr($value = null, $data = [])
    {
        $status = [0 => "<span class='badge bg-red'>禁用</span>", 1 => "<span class='badge bg-green'>启用</span>"];
        return $status[$data['status']] ?? "<span class='badge bg-gray'>未知</span>";
    }
    
    /**
     * 更新分类路径和层级
     * @param int $categoryId 分类ID
     * @param int $parentId 父分类ID
     * @return bool
     */
    public function updateCategoryPath($categoryId, $parentId = 0)
    {
        if ($parentId > 0) {
            $parent = $this->getInfo(['id' => $parentId], 'path,level');
            if (!$parent) {
                return false;
            }
            $path = $parent['path'] . ',' . $categoryId;
            $level = $parent['level'] + 1;
        } else {
            $path = $categoryId;
            $level = 1;
        }
        
        return $this->updateInfo(['id' => $categoryId], [
            'path' => $path,
            'level' => $level
        ]);
    }
    
    /**
     * 获取分类路径数组
     * @param string $path 路径字符串，如 "1,3,5"
     * @return array
     */
    public function getCategoryPathArray($path)
    {
        if (empty($path)) {
            return [];
        }
        
        $pathIds = explode(',', $path);
        $pathIds = array_filter($pathIds);
        
        if (empty($pathIds)) {
            return [];
        }
        
        $categories = $this->getList(['id' => ['in', $pathIds]], 'id,name', '', false);
        
        if (!$categories) {
            return [];
        }
        
        // 按路径顺序排列
        $sortedCategories = [];
        foreach ($pathIds as $id) {
            foreach ($categories as $category) {
                if ($category['id'] == $id) {
                    $sortedCategories[] = $category;
                    break;
                }
            }
        }
        
        return $sortedCategories;
    }
    
    /**
     * 递归更新子分类的路径和层级
     * @param int $parentId 父分类ID
     * @return bool
     */
    public function updateChildrenPath($parentId)
    {
        $children = $this->getList(['parent_id' => $parentId], 'id,parent_id', '', false);
        
        if (!$children) {
            return true;
        }
        
        foreach ($children as $child) {
            // 更新当前子分类的路径
            $this->updateCategoryPath($child['id'], $parentId);
            
            // 递归更新子分类的子分类
            $this->updateChildrenPath($child['id']);
        }
        
        return true;
    }

}